#include "Arduino.h"
#include "config.h"

bool isFirstUse = false;

void checkFirstUse() {
  long value;
  EEPROM.get(999, value);

  if (value == 31415926L) {
    Serial.println("is not first use 1111");
    isFirstUse = false;
  } else {
    isFirstUse = true;
    Serial.println("first use 0000");

    // 不需要手动调用 EEPROM.commit()，EEPROM.put() 会自动提交
    EEPROM.put(999, 31415926L);
    Serial.println("EEPROM write success");
  }
}

void everySecondWork() {
  // Serial.println(LDRReadValue());
  monitorLDR();
  monitorDHT();
  if (timeinfo != nullptr) {
    timeinfo->tm_sec++;
    if (timeinfo->tm_sec == 60) {
      timeinfo->tm_sec = 0;
      timeinfo->tm_min++;
      if (timeinfo->tm_min == 60) {
        timeinfo->tm_hour++;
        if (timeinfo->tm_hour == 24) {
          timeinfo->tm_mday++;
        }
      }
    }
  }
}

void everyMinuteWork() {
  Serial.println("everyMinuteWork");
  if (timeinfo != nullptr) {
    Serial.printf("%d %d\n",timeinfo->tm_hour,timeinfo->tm_min);
    lv_label_set_text(ui_date, (String(timeinfo->tm_year) + "/" + String(timeinfo->tm_mon) + "/" + String(timeinfo->tm_mday)).c_str());
    lv_label_set_text(ui_time, (String(timeinfo->tm_hour) + ":" + String(timeinfo->tm_min)).c_str());
  }
  if (clockWork()) {
    I2S_play_file(sdRandomOpen("/clock"));
  }
}

void everyHourWork() {
  updateMonitorDHT();
  updateMonitorLDR();
  isWeatherAndTimeUpdate = false;
}

void everyLoopWork(){
  ASRLoop();
}